package com.lwh.controller;

import com.lwh.entity.MdCheliangEntity;
import com.lwh.service.MdCheliangService;
import com.lwh.util.EasyPoiUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Validator;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;


/**
 * @Title: Controller
 * @Description: 车辆管理
 * @author
 * @date
 * @version
 *
 */
@Controller
@RequestMapping("/mdCheliang")
public class MdCheliangController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(MdCheliangController.class);

	@Autowired
	private MdCheliangService mdCheliangService;

	@Autowired
	private Validator validator;

	// 查询页面内容
	@RequestMapping("/findPage")
	@ResponseBody
	public Callable<Map<String,Object>> findPage(MdCheliangEntity query){

		if(query.getPage()==null) {
			query.setPage(1);
		}
		if(query.getRows()==null) {
			query.setRows(10);
		}
		Callable<Map<String,Object>> callable =(()->mdCheliangService.findPage(query));
		return callable;
	}

	/**
	 * 增加功能
	 * @param
	 * @return
	 */
	@PostMapping("/addCheliang")
	@ResponseBody
	public Callable<String> add(MdCheliangEntity mdCheliangEntity){

		Callable<String> callable =null;

		try {
			mdCheliangService.insertSelective(mdCheliangEntity);
			callable = ()->{return "success";};
		} catch (Exception e) {
			e.printStackTrace();
			callable = ()->{return "系统出错了";};
		}
		return callable;
	}

    // 修改
	@PostMapping("/updateCheliang")
	@ResponseBody
	public Callable<String> update(MdCheliangEntity mdCheliangEntity){

		Callable<String> callable =null;
		if(mdCheliangEntity.getId() == null) {
			callable = ()->{return "系统缺少参数";};
			return callable;
		}
		try {
			callable = ()->{
				mdCheliangService.updateByPrimaryKeySelective(mdCheliangEntity);
				return "success";
			};
		} catch (Exception e) {
			e.printStackTrace();
			callable = ()->{return "系统发生错误";};
		}
		return callable;
	}



	// 批量删除
	@RequestMapping("/deleteCheliang")
	@ResponseBody
	public Callable<String> delete(String[] ids){
		Callable<String> callable =null;
		if(ids == null || ids.length==0) {
			callable = ()->{return "系统缺少参数";};
			return callable;
		}
		try {
			callable = ()->{
				mdCheliangService.deleteByIds(ids);
				return "success";
			};
		} catch (Exception e) {
			e.printStackTrace();
			callable = ()->{return "系统发生错误";};
		}
		return callable;
	}

	// 单独删除
	@RequestMapping("/deleteOneCheliang")
	@ResponseBody
	public Callable<String> deleteOne(String id){
		Callable<String> callable =null;
		if(id == null ) {
			callable = ()->{return "系统缺少参数";};
			return callable;
		}
		try {
			callable = ()->{
				mdCheliangService.deleteOneById(id);
				return "删除成功";
			};
		} catch (Exception e) {
			e.printStackTrace();
			callable = ()->{return "系统发生错误";};
		}
		return callable;
	}


	// 导入
	@RequestMapping("/importExcel")
	@ResponseBody
	public String importExcel(MultipartFile uploadFile){

		String result = mdCheliangService.importExcel(uploadFile);

		return result;

	}

	// 导出
	@RequestMapping("/exportExcel")
	@ResponseBody
	public void outExcel(MdCheliangEntity mdCheliangEntity, HttpServletResponse response){

		String result = null;
		try {
			String fileName = "车辆管理";
			List<MdCheliangEntity> list = mdCheliangService.outExcel(mdCheliangEntity);
			EasyPoiUtil.exportExcel(list,fileName,fileName,MdCheliangEntity.class,fileName+".xls",true,response);
			result = "success";
		} catch (Exception e) {
			e.printStackTrace();
			result = "fail";
		}finally {

		}
	}

	// 模板下载
	@RequestMapping("/modelExcel")
	@ResponseBody
	public void modelExcel(HttpServletResponse response){

		String result = null;
		List<MdCheliangEntity> list = new ArrayList();
		try {
			String fileName = "车辆管理";
			EasyPoiUtil.exportExcel(list,fileName,fileName,MdCheliangEntity.class,fileName+".xls",true,response);
			result = "success";
		} catch (Exception e) {
			e.printStackTrace();
			result = "fail";
		}finally {

		}
	}

}
